package cn.com.css.taxInspection.zzgl.service.impl;

import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.zzgl.entity.YhglEntity;
import cn.com.css.taxInspection.zzgl.mapper.YhglMapper;
import cn.com.css.taxInspection.zzgl.service.YhglService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("YhglService")
public class YhglServiceImpl extends ServiceImpl<YhglMapper, YhglEntity> implements YhglService {

    @Resource
    private HttpSessionService httpSessionService;

    @Resource
    private YhglMapper yhglMapper;

    @Override
    public IPage<YhglEntity> queryYhglData(YhglEntity yhglEntity) {
        IPage<YhglEntity> page = new Page<>(yhglEntity.getPage(), yhglEntity.getLimit());
        String swjgDm = httpSessionService.getLogSwjgDm();
        LambdaQueryWrapper<YhglEntity> queryWrapper = Wrappers.lambdaQuery();
        if (TsBqUtils.isNull(yhglEntity.getSwjgDm())) {
            queryWrapper.like(YhglEntity::getSwjgDm, swjgDm.substring(0, 9));
        } else if (swjgDm.equals(yhglEntity.getSwjgDm())) {
            queryWrapper.like(YhglEntity::getSwjgDm, swjgDm.substring(0, 9));
        } else {
            queryWrapper.like(YhglEntity::getSwjgDm, yhglEntity.getSwjgDm());
        }
        if (TsBqUtils.isNotNull(yhglEntity.getSwryDm())) {
            queryWrapper.like(YhglEntity::getSwryDm, yhglEntity.getSwryDm());
        }
        if (TsBqUtils.isNotNull(yhglEntity.getSwryxm())) {
            queryWrapper.like(YhglEntity::getSwryxm, yhglEntity.getSwryxm());
        }
        queryWrapper.eq(YhglEntity::getZsfbz, "Y");
        queryWrapper.orderByAsc(YhglEntity::getSwjgDm);
        return page(page, queryWrapper);
    }

    @Override
    public YhglEntity queryYhglDataBySwryDm(String swryDm) {
        LambdaQueryWrapper<YhglEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(YhglEntity::getSwryDm, swryDm);
        queryWrapper.eq(YhglEntity::getZsfbz, "Y");
        return this.getOne(queryWrapper);
    }

    @Override
    public YhglEntity queryYhglDataBySwrysfDm(String swrysfDm) {
        LambdaQueryWrapper<YhglEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(YhglEntity::getRysfDm, swrysfDm);
        queryWrapper.eq(YhglEntity::getZsfbz, "Y");
        return this.getOne(queryWrapper);
    }

    @Override
    public List<YhglEntity> queryYhglDataBySwjgDm(String swjgDm) {
        if (TsBqUtils.isNull(swjgDm)) {
            swjgDm = httpSessionService.getLogSwjgDm();
        }

        LambdaQueryWrapper<YhglEntity> queryWrapper = Wrappers.lambdaQuery();

        queryWrapper.select(
                YhglEntity::getSwjgDm,
                YhglEntity::getSwjgmc
        );

        // 添加like条件
        queryWrapper.like(YhglEntity::getSwjgDm, swjgDm.substring(0, 9));

        // Y 是从 金三过来的， Z表示 自定义的机关或者人员
        queryWrapper.and(wrapper -> wrapper
                .eq(YhglEntity::getRyzt, "Y")
                .or()
                .eq(YhglEntity::getRyzt, "Z")
        );

        // 链式调用，避免多次调用 groupBy
        queryWrapper.groupBy(YhglEntity::getSwjgDm).groupBy(YhglEntity::getSwjgmc);
        return this.list(queryWrapper);
    }

    @Override
    public int updataYhglData(YhglEntity yhglEntity) {
        LambdaQueryWrapper<YhglEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(YhglEntity::getSwryDm, yhglEntity.getSwjgDm());
        List<YhglEntity> yhglEntityList = yhglMapper.selectList(queryWrapper);
        //1.先把所有的身份标志都改成 N
        if (TsBqUtils.isNotNull(yhglEntityList)) {
            for (YhglEntity yhglEntity1 : yhglEntityList) {
                yhglEntity1.setZsfbz("N");
                yhglMapper.updateById(yhglEntity1);
            }
        }
        //2.更新选中的身份机关 为 Y
        return yhglMapper.updateById(yhglEntity);
    }

    @Override
    public int saveYhglData(YhglEntity yhglEntity) {
        //1.根据税务人员代码，查询税务人员身份代码
        LambdaQueryWrapper<YhglEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(YhglEntity::getSwryDm, yhglEntity.getSwryDm());
        queryWrapper.orderByDesc(YhglEntity::getRysfDm);
        List<YhglEntity> oldYhglEntityList = yhglMapper.selectList(queryWrapper);
        //2.如果他不是空，就跟他加一个身份
        if (TsBqUtils.isNotNull(oldYhglEntityList)) {
            for (YhglEntity entity : oldYhglEntityList) {
                entity.setZsfbz("N");
                entity.setRyzt("Z");
                yhglMapper.updateById(entity);
            }
            long maxValue = Long.parseLong(oldYhglEntityList.get(0).getRysfDm()) + 1;
            yhglEntity.setRysfDm(String.valueOf(maxValue));
        } else {
            yhglEntity.setRysfDm(yhglEntity.getSwryDm() + "01");
        }
        return yhglMapper.insert(yhglEntity);
    }

    @Override
    public int delDataBySwryDm(String swrysfDm) {
        return yhglMapper.deleteById(swrysfDm);
    }

}